package main

import (
	"fmt"
	"log"
	"mygoedu/day11/socket"
	"net"
	"regexp"
	"strconv"
	"strings"
	"time"
)

func main() {
	ip := "52.68.138.136"
	port := 6058

	addr, err := net.ResolveTCPAddr("tcp", ip+":"+strconv.Itoa(port))
	socket.CheckError(err)

	conn, err := net.DialTCP("tcp", nil, addr)
	socket.CheckError(err)

	resp, err := conn.Write([]byte("Action: login\r\nUserName: dishui\r\nSecret: 7moorcom\r\n\n"))

	for {
		resp, err = conn.Write([]byte("ACTION: COMMAND\ncommand: core Show Channels\n\n"))
		log.Printf("send %d to server: ", resp)
		response := make([]byte, 65536)
		time.Sleep(2 * time.Second)
		_, err = conn.Read(response)
		result := string(response)
		//log.Printf("receive from server:\n %s", result)
		r, _ := regexp.Compile(`[0-9]{1,} active calls`)
		fmt.Println(strings.Split(r.FindString(result), " ")[0])
	}

	conn.Close()
}
